package com.tanhua.autoconfig.template;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

public class FaceTemplate {
    @Autowired
    private AipFace client;

    public boolean detect(String imgUrl) {
        //设置APPID/AK/SK
//        String APP_ID = "24855806";
//        String API_KEY = "tfHhITCXBz304P1qvkCi0PT7";
//        String SECRET_KEY = "XFKxhIkzzZeKTluthOhjK501uUcsKwOX";
        // 初始化一个AipFace
//        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
//        client.setConnectionTimeoutInMillis(2000);
//        client.setSocketTimeoutInMillis(60000);

//        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 调用接口
//        String image = imgUrl;
        String imageType = "URL";
        //可选
//        HashMap<String, String> options = new HashMap<String, String>();
//        options.put("face_field", "age");
//        options.put("max_face_num", "2");
//        options.put("face_type", "LIVE");
//        options.put("liveness_control", "LOW");



        // 人脸检测
        JSONObject res = client.detect(imgUrl, imageType, null);
        System.out.println(res.toString(2));
        /*
        * {
                "result": null,
                "log_id": 9455458479941,
                "error_msg": "param[image_type] format error",
                "cached": 0,
                "error_code": 222015,
                "timestamp": 1631706276
            }
        * */
        Integer error_code = (Integer) res.get("error_code");
//        System.out.println(res);
        return error_code == 0;
    }

    public static void main(String[] args) {
    }

}
